package com.enjoyor.health.HGPT.util;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author Zhu Min
 * @date 2023/12/26 16:17
 */
@Component
public class SmsUtil {

    @Value("${sms.aliyun.ACCESS_KEY_Id}")
    private  String ACCESS_KEY_Id;
    @Value("${sms.aliyun.ACCESS_KEY_SECRET}")
    private  String ACCESS_KEY_SECRET;
    @Value("${sms.aliyun.REGION_ID}")
    private  String REGION_ID;
    //@Value("${sms.aliyun.SIGN_NAME}")
    private  String SIGN_NAME = "银江技术";
    @Value("${sms.aliyun.TEMPLATE_CODE}")
    private  String TEMPLATE_CODE;

    public JSONObject senMassage(String phoneNumber, String code) {
        JSONObject result = new JSONObject();
        //随机生成6位验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", ACCESS_KEY_Id, ACCESS_KEY_SECRET);
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", REGION_ID);
        request.putQueryParameter("PhoneNumbers", phoneNumber);
        request.putQueryParameter("SignName", SIGN_NAME);
        request.putQueryParameter("TemplateCode", TEMPLATE_CODE);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            result = JSON.parseObject(response.getData());
            result.put("verCode", code);
            return result;
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return result;
    }
}


